package com.woniuxy.store.service.impl;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.store.infra.mapper.StoreWalletMapper;
import com.woniuxy.store.infra.model.StoreWallet;
import com.woniuxy.store.infra.model.StoreWithdrawRecord;
import com.woniuxy.store.service.StoreWalletService;
import com.woniuxy.store.service.StoreWithdrawRecordService;
import exception.MallException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2023-07-20
 */
@Service
public class StoreWalletServiceImpl extends ServiceImpl<StoreWalletMapper, StoreWallet> implements StoreWalletService {



    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public StoreWallet getWallet(Integer storeId) {
        StoreWallet wallet = getOne(new QueryWrapper<StoreWallet>().eq("store_id", storeId));
        return wallet;
    }

    @Override
    public void addBalance(Integer storeId, Integer storeWalletId, BigDecimal amount) {
        StoreWallet wallet = getById(storeWalletId);
        if (wallet==null||wallet.getStoreId().intValue()!=storeId.intValue()){
            throw new MallException("找不到该账户", CommonResultCodeEnum.INVALID_PARAM);
        }
        //账户余额增加
        BigDecimal newBalance = wallet.getStoreBalance().add(amount);
        wallet.setStoreBalance(newBalance);
        updateById(wallet);

    }

    @Override
    public void withdraw(Integer storeWalletId, Integer storeId, BigDecimal amount) {
        //前置判断
        StoreWallet wallet = getById(storeWalletId);
        if (wallet==null||wallet.getStoreId().intValue()!=storeId.intValue()){
            throw new MallException("找不到该账户", CommonResultCodeEnum.INVALID_PARAM);
        }

        //手续费计算
        BigDecimal transferFee = amount.multiply(BigDecimal.valueOf(wallet.getWithdrawalRate()));
        //总减少金额
        BigDecimal newAmount = amount.add(transferFee);
        //判断提现金额是否小于等于余额
        if (newAmount.compareTo(wallet.getStoreBalance()) > 0){
            throw new MallException("余额不足",CommonResultCodeEnum.INVALID_PARAM);
        }
        //扣减余额
        wallet.setStoreBalance(wallet.getStoreBalance().subtract(newAmount));
        updateById(wallet);

        //创建消息队列操作提现记录
        StoreWithdrawRecord record = new StoreWithdrawRecord();
        record.setStoreId(storeId);
        record.setStoreWalletId(storeWalletId);
        record.setAmount(newAmount);
        record.setActualArrival(amount);
        record.setTransferFee(transferFee);
        record.setWithdrawTime(new Date());
        Message<StoreWithdrawRecord> withdraw = MessageBuilder.withPayload(record).build();
        rocketMQTemplate.syncSend("withdraw_record", withdraw);

    }
}
